﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ISS.Models;
using Utils;
using System.Data;

namespace ISS.Business
{
    public class ImageDAO
    {
        private ISSContext context = new ISSContext();

        public int Create(Images a)
        {
            try
            {
                a.Alias = Utils.Format.ConvertToNoSign(a.Title);
                a.UploadDate = DateTime.Now;
                a.Like = 0;
                a.ViewCount = 0;
                context.dbImage.Add(a);
                return context.SaveChanges();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return -1;
            }
        }

        public int Update(Images a)
        {
            try
            {
                context.Entry(a).State = EntityState.Modified;
                return context.SaveChanges();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return -1;
            }
        }

        public int Delete(int imageId)
        {
            try
            {
                Images image = context.dbImage.Find(imageId);
                context.dbImage.Remove(image);
                return context.SaveChanges();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return -1;
            }

        }

        public IEnumerable<Images> GetAll()
        {
            try
            {
                return context.dbImage.ToList();
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public IEnumerable<Images> GetByAlbum(int albumId)
        {
            try
            {
                return from image in context.dbImage where image.AlbumId == albumId select image;
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public IEnumerable<Images> GetByUser(int userId)
        {
            try
            {
                return from image in context.dbImage where image.Album.Creator == userId select image;
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public IEnumerable<Images> GetByUser(int userId, int pageIndex, int pageSize = 20)
        {
            try
            {
                return (from image in context.dbImage where image.Album.Creator == userId select image).Skip(pageIndex * 10).Take(pageSize);
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public IEnumerable<Images> GetLastest(int userId, int total = 20)
        {
            try
            {
                return (from image in context.dbImage where image.Album.Creator == userId orderby image.UploadDate descending select image).Skip(0).Take(20);
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }

        public Images GetById(int imageId)
        {
            try
            {
                Images img = (from image in context.dbImage where image.ImageId == imageId select image).First();
                img.CommentCount = (from comment in context.dbImageComment where comment.ImageId == imageId select comment).Count();
                return img;
            }
            catch (Exception ex)
            {
                Logging.WriteError(ex);
                return null;
            }
        }
    }
}